package com.atguigu.spzx.utils;

import com.atguigu.spzx.model.entity.system.SysMenu;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
//封装树形结构
public class MenuHelper {
    public static List<SysMenu> bouildTree(List<SysMenu> sysMenuList){
        //TODO 完成封装过程
        List<SysMenu> trees = new ArrayList<>();
        //递归实现封装

        //遍历所有菜单
        for (SysMenu item:sysMenuList
             ) {
            //找到递归入口
            if (item.getParentId() == 0){
                //查找下一层菜单
                trees.add(findChildren(item,sysMenuList));
            }
        }
        return trees;
    }

    public static SysMenu findChildren(SysMenu item,List<SysMenu> sysMenuList){
        //设置下级菜单
        item.setChildren(new ArrayList<SysMenu>());
        //循环数据
        for (SysMenu menu:sysMenuList
             ) {
            //循环出来的单个菜单信息
            if (menu.getParentId().longValue() == item.getId().longValue()){
                //如果相等说明当前菜单item的下级菜单
                item.getChildren().add(findChildren(menu,sysMenuList));
            }
        }
        return item;
    }
}
